package com.atgg.config;
// 配置类 == 配置文件

import com.atgg.beans.Color;
import com.atgg.beans.ColorFactory;
import com.atgg.beans.Person;
import com.atgg.condition.LinuxCondition;
import com.atgg.condition.MyImportBeanDefinitionRegistrar;
import com.atgg.condition.MyImportSelector;
import com.atgg.condition.WindowsCondition;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Configuration
//扫描排除哪些包
//@ComponentScan(value = "com.atgg",excludeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,Repository.class})
//})
//@ComponentScan(value = "com.atgg")

//@Conditional(value = LinuxCondition.class) // 给类统一标识
// 扫描包含哪些包

@ComponentScan(value = "com.atgg",includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Repository.class,Service.class})},useDefaultFilters = false)
@Import(value = {Color.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})// 快速导入组件
public class MainCofig {
    //给容器注册一个bean，类型为返回值的类型，id默认为方法名
//    @Lazy
    @Bean(name = "iflytek")
    public Person person(){
        return new Person("WANG",26);
    }

    // @Conditional按一定条件进行判断，满足条件给容器中注册bean
    @Conditional(value = LinuxCondition.class)
    @Bean(value = "bill")
    public Person person02(){
        return new Person("bill", 24);
    }
    @Conditional(value = {WindowsCondition.class})
    @Bean(value = "kill")
    public Person person01(){
        return new Person("kill", 26);
    }


    /**
     * 给容器中注册组件：
     * 1.包扫描+组件标注注解（@Service, @Repository,）
     * 2.@Bean[导入第三方组件]
     * 3.@Import
     *          ①@Import(value = {Color.class})快速导入,bean名称为 全类名
     *          ②ImportSelector:返回需要导入的全类名数组 [重点]
     *          ③ImportBeanDefinitionRegistrar：手动注册bean到容器中去
     *  4.使用spring提供的FactoryBean【重点】
     *      （1）默认获取到的是工厂bean创建的对象，即getObject方法获取的对象；
     *      （2）要获取工厂bean本身，我们需要给id前面加上&
     *     &ColorFactory-------->    class com.atgg.beans.ColorFactory
     *
     */
@Bean
public ColorFactory colorFactory (){
    return new ColorFactory();

}


}
